package ChatSystem.ServerDemo.frame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 设置端口号窗体
 * @create 2022.5.29
 * @author Administrator
 *
 */
public class ServerSetPost extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1L;

	//确定按钮
	private JButton set = new JButton("设置端口号");
	
	//关闭窗口按钮
	private JButton exit = new JButton("关闭");
	
	//窗口输入
	private JTextField write = new JTextField();
	
	//端口号
	private JLabel post = new JLabel("端口号");
	
	public ServerSetPost() {
		//初始化
		init();
	}
	
	private void init(){
		
		//设置窗口名称
		this.setTitle("服务器");
		
		//窗口大小固定
		this.setResizable(false);
		
		//设置窗口关闭
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//设置字体
		exit.setFont(new Font("宋体",1,18));
		set.setFont(new Font("宋体",1,18));
		write.setFont(new Font("宋体",1,18));
		post.setFont(new Font("宋体",1,18));
		
		//添加监听
		exit.addActionListener(this);
		set.addActionListener(this);
		
		//设置布局
		this.setLayout(null);
		this.setBounds(600,300,400,280);
		set.setBounds(50,150,150,30);
		exit.setBounds(250,150,80,30);
		write.setBounds(100,80,200,20);
		post.setBounds(30,80,150,20);
		
		//添加组件
		this.add(exit);
		this.add(set);
		this.add(write);
		this.add(post);
		
		//窗体可见
		this.setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		JButton button = (JButton) e.getSource();
		if(button.equals(exit)){//退出
			System.exit(0);
		}else if(button.equals(set)){//确认
			String message = write.getText();
			int post;
			try{
				post = Integer.parseInt(message);
			}catch(NumberFormatException exception){
				JOptionPane.showMessageDialog(null, "请输入整数");
				write.setText("");
				return;
			}
			this.dispose();
			new ServerMainFrame(post);
		}
	}
	
}
